// 相关接口
import { getDepart } from "../api/depart.js";
import { findDepartAndPosition } from "../api/salary.js"

/**
 * @param {string} str
 * @returns {Boolean}
 * 验证用户名
 */
export function validUsername(str) {
    const username = 'guguyu'
    return username == str.trim();
}

/**
 * @param {string} str
 * @returns {Boolean}
 * 验证密码
 */
export function validPassword(str) {
    const psd = 'guguyu168'
    return psd == str.trim();
}

/**
 * 
 * @param {string} str 
 * @returns {Boolean}
 * 验证电话号码
 */
export function validTel(str) {
    // console.log(str);
    let reg = /^1[3|4|5|7|8][0-9]{9}$/;
    // console.log(reg.test(str));
    return reg.test(str);
}

/**
 * 
 * @param {string} str 
 * @returns {Boolean}
 * 验证邮箱
 */
export function validEmail(str) {
    // console.log(str);
    let reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    // console.log(reg.test(str));
    return reg.test(str);
}

/**
 * 
 * @param {string} str 
 * @returns {Boolean}
 * 验证添加的部门是否存在
 */
export async function validDepartName(str) {
    // console.log(str);
    let infos = await getDepart();
    let departs = [];
    for (let val in infos.data) {
        departs[val] = infos.data[val].name;
    }
    // console.log(departs.indexOf(str) == -1);
    return departs.indexOf(str) == -1;
}

/**
 * 
 * @param {string} str 
 * @returns {Boolean}
 * 验证部门电话号码（座机）
 */
export function validDepartTel(str) {
    let reg = /^[\d]{3,4}[-][\d]{7,8}$/;
    return reg.test(str);
}

/**
 * 
 * @param {string} str 
 * @returns {Boolean}
 * 验证添加的薪资是否重复
 */
export async function validSalaryMsg(department, position) {
    let infos = await findDepartAndPosition({ department, position });
    return infos.data.length;
}
